;(function () {
  var projectIds = hlUrlUtil.GetQueryString('projectIds');
  new Vue({
    el: '#todoIndex',
    data: {
      data: [],
      todoState: [],
      pageNums: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      info: '',
      limit: ['10'],
      pageNum: {
        totalPage: 0,
        totalCount: 0
      },
			projectList: [],
			buildingList: [],
			floorList: [],
			unitList: [],
			projectId: [],
			buildingId: [],
			floorId: [],
			unitId: [],
			showView: false,
			selectedData: []
    },
    filters: {
      tozero: function (n) {
        return n < 10 ? '0' + n : '' + n;
      },
      timeFilter: function(time) {
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd HH:mm:ss')
      }
    },
    methods: {
			checkBpm (arr) {
				this.getData(1, arr[0].code)
			},
      //获取流程唯一标识列表
      bpm: function () {
        var _this = this;
        hlhttp.get({
          url: 'bpm_keys',
          success: function (res) {
						res.unshift({value: "#", memo: "全部"})
            let newData = []
            let userinfo = JSON.parse(localStorage.getItem('userMes'))
            newData = hlArrayUtil.replaceData(res, [{key: 'memo', rKey: 'name'},{key: 'value', rKey: 'code'}])
            if (userinfo.preSysCode.indexOf('SINOOCEAN') == -1) {
            	newData.map((item, index) => {
            		if (item.dictValue == 'RRAT') {
            			newData.splice(index, 1)
            		}
            	})
            }
            _this.todoState = newData

						var page = hlUrlUtil.GetQueryString('stateName');
						_this.selectedData = [page]
            _this.pagesearch(page)
          }
        })
      },
      //获取待办事项列表数据
      getData: function (nums, state) {
        var _this = this;
        hlhttp.get({
          url: 'bpm/todo_tasks',
          data: {
            bpm_key: state,
            keyword: this.info.trim() || '#',
            page: nums,
            limit: this.limit[0],
            project_ids:projectIds || '#',
						project_id: this.projectId.length ? this.projectId[0] : '#',
						building_id: this.buildingId.length ? this.buildingId[0] : '#',
						floor_id: this.floorId.length ? this.floorId[0] : '#',
						unit_id: this.unitId.length ? this.unitId[0] : '#',
          },
          success: function(res) {
            _this.pageNum = {totalPage: res.totalPage, totalCount:res.totalCount}
            _this.data = res.tasks;
            $("#page").initPage(res.totalPage, nums, function(n) {
              if (nums == n) {
                return
              }
              _this.getData(n, state);
            });
          }
        })
      },
      pagesearch: function(state) {
				var _this = this;
				setTimeout(() => {
					var pages = {
						"turnPage": function(n) {
							_this.getData(n, state);
						}
					}
					$("#page").initPage(1, 1, pages.turnPage);
				}, 1000);
      },
      getDateTime: function(arr) {
				this.limit = [arr[0].code]
        var _this = this;
        setTimeout(function() {
          var key = _this.selectedData[0], state;
          _this.todoState.map(item => {
						if (item.memo == key) {
							state = item.code
						}
					})
          _this.getData(1, state)
        }, 100);
      },
      reset: function () {
        this.info = '';
				this.projectId = []
				this.buildingId = []
				this.floorId = []
				this.unitId = []
				this.buildingList = []
				this.floorList = []
				this.unitList = []
				this.selectedData = ['全部']
        this.getData(1, this.todoState[0].code)
      },
      // 前往合同流程
      goToContractFLow:function (taskId,bpmId,bpmKey,businessId) {
        var parame = {
          projectIds:projectIds,
          taskId:taskId,
          bpmId:bpmId,
          bpmKey:bpmKey,
          businessId:businessId,
          ddtab:true
        }
        hlUrlUtil.goTo('contractFLow',parame)
      },
      // 前往合同流程
      goToITFLow:function (taskId,bpmId,bpmKey,businessId) {
        var parame = {
          projectIds:projectIds,
          taskId:taskId,
          bpmId:bpmId,
          bpmKey:bpmKey,
          businessId:businessId,
          ddtab:true
        }
        hlUrlUtil.goTo('intentionFLow',parame)
      },
			// 前往远洋流程
			rratFlowPage:function (taskId,bpmId,bpmKey,businessId) {
			  var parame = {
			    projectIds:projectIds,
			    taskId:taskId,
			    bpmId:bpmId,
			    bpmKey:bpmKey,
			    businessId:businessId,
			    ddtab:true
			  }
			  hlUrlUtil.goTo('rratFlow',parame)
			},
			// 前往变更流程
			cctFlowPage:function (taskId,bpmId,bpmKey,businessId) {
			  var parame = {
			    projectIds:projectIds,
			    taskId:taskId,
			    bpmId:bpmId,
			    bpmKey:bpmKey,
			    businessId:businessId,
			    ddtab:true
			  }
			  hlUrlUtil.goTo('contractFlowChange',parame)
			},
			// 前往账单流程
			btFlowPage:function (taskId,bpmId,bpmKey,businessId) {
			  var parame = {
			    projectIds:projectIds,
			    taskId:taskId,
			    bpmId:bpmId,
			    bpmKey:bpmKey,
			    businessId:businessId,
			    ddtab:true
			  }
			  hlUrlUtil.goTo('billFlow',parame)
			},
      // 前往退租流程
      goToWithdrawFlow:function (taskId,bpmId,bpmKey,businessId) {
        var parame = {
          projectIds:projectIds,
          taskId:taskId,
          bpmId:bpmId,
          bpmKey:bpmKey,
          businessId:businessId,
          ddtab:true
        }
        hlUrlUtil.goTo('withdrawFlow',parame)
      },
			openSearch: function() {
			  if (this.showView == true) {
			    this.showView = false;
					this.projectId = '#'
					this.buildingId = '#'
					this.floorId = '#'
					this.unitId = '#'
					this.buildingList = []
					this.floorList = []
					this.unitList = []
			    return false;
			  }
			  if (this.showView == false) {
			    this.showView = true;
			    return false;
			  }
			},
			//获取项目
			getprojectData () {
			  hlhttp.get({
			    url: 'user/projects',
			    success: (res) => {
						res = hlArrayUtil.removeRepeat(res, 'projectId')
			      this.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼宇
			getbuildingData () {
			  hlhttp.get({
			    url: 'project/buildings',
			    data: {project_id: this.projectId[0]},
			    success: (res) => {
			      this.buildingList = hlArrayUtil.replaceData(res, [{key: 'buildingId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼层
			getfloorData () {
			  hlhttp.get({
			    url: 'building/floors',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
			    success: (res) => {
			      this.floorList = hlArrayUtil.replaceData(res, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
			    }
			  })
			},
			//获取单元
			getunitData () {
			  hlhttp.get({
			    url: 'floor/units',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
			    success: (res) => {
			      this.unitList = hlArrayUtil.replaceData(res, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
			    }
			  })
			},
			//点击项目获得  id
			getprojectId (arr) {
				console.log(arr)
			  this.projectId = [arr[0].code];
			  this.getbuildingData()
			  this.buildingId = [];
			  this.floorId = [];
			  this.unitId = [];
			},
			//点击楼宇获得 id
			getbuildingId (arr) {
			  this.buildingId = [arr[0].code];
			  this.getfloorData()
			  this.floorId = [];
			  this.unitId = [];
			},
			//点击楼层获得 id
			getfloorId (arr) {
			  this.floorId = [arr[0].code];
			  this.getunitData()
			  this.unitId = [];
			},
			// 点击单元获得 id
			getunitId (arr) {
			  this.unitId = [arr[0].code];
			},
    },
    mounted: function () {
      hlSelectUi(true)
      this.bpm()
			this.getprojectData()
    }
  })
} ())
